<template>
  <div class="table">
    <el-table
      :data="tableData"
      :loading="loading"
      style="width: 100%"
      :header-cell-style="{
        background: '#F1F1F1',
        'font-size': '14px',
        'font-weight': 600,
        color: '#333333',
      }"
      :cell-style="{
        'font-size': '14px',
        'font-weight': 400,
        color: '#333333',
      }"
    >
      <el-table-column label="序号" min-width="60" fixed="left">
        <template #default="scope">
          {{ (scope.$index + 1).toString().padStart(2, "0") }}
        </template>
      </el-table-column>
      <el-table-column prop="comName" label="物业名称" min-width="140" />
      <el-table-column prop="name" label="场库名称" min-width="140" />
      <el-table-column prop="totalSpaces" label="总泊位数（个）" min-width="140" />
      <el-table-column prop="address" label="所属位置" min-width="300" />
      <el-table-column label="运营状态" min-width="80">
        <template #default="scope">
          {{
            scope.row.status == 1
              ? "营业中"
              : scope.row.status == 2
              ? "车位已满"
              : scope.row.status == 3
              ? "已停用"
              : "-"
          }}
        </template>
      </el-table-column>
      <el-table-column label="启用状态" min-width="80">
        <template #default="scope">
          <el-switch
            width="50px"
            :inline-prompt="true"
            v-model="scope.row.enabled"
            :active-value="true"
            :inactive-value="false"
            @change="handleStatusChange(scope.row)"
          />
          <!-- {{ scope.row.enabled == 1 ? "启用中" : scope.row.enabled == 0 ? "已停用" : "-" }} -->
        </template>
      </el-table-column>
      <el-table-column label="操作" width="60" fixed="right">
        <template #default="scope">
          <el-button
            @click="detailData(scope.row)"
            type="primary"
            size="small"
            link
            style="color: #007aff; font-size: 14px"
            >详情</el-button
          >
        </template>
      </el-table-column>
      <template #empty>
        <el-empty :image="imgNone" />
      </template>
    </el-table>
  </div>
</template>

<script setup>
import { ElMessageBox } from "element-plus";
import { reactive, ref, defineProps } from "vue";
import { getEnabledAPi } from "../request.js";
const imgNone = new URL("@/assets/image/imgNone.png", import.meta.url).href;
const emits = defineEmits(["table"]);

const props = defineProps({
  tableData: {
    type: Object,
    default: () => {},
  },
  loading: {
    type: Boolean,
    default: false,
  },
});

// 启用/停用状态按钮
const handleStatusChange = (row) => {
  ElMessageBox.confirm(
    `您确认要${row.enabled == true ? "启用" : "停用"}（${row.name}）吗？`,
    "温馨提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(async () => {
      await getEnabledAPi({
        id: row.id,
        enabled: row.enabled,
      }).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
        } else if (res.data.code === -1) {
          row.enabled = row.enabled === true ? false : true;
        }
      });
    })
    .catch(() => {
      row.enabled = row.enabled === true ? false : true;
    });
};

const detailData = (row) => {
  row.operation = "detail";
  emits("table", row);
}
</script>

<style lang="less" scoped>
.table {
  margin-bottom: 15px;
}
</style>
